const koa = require('koa');
const app = new koa();

// 解决跨域问题
const cors = require('koa2-cors');
app.use(
    cors({
        origin: ['http://localhost:3000'], // 允许跨域的地址
        credentials: true, // 证书
    })
);

// 接受前端get请求
const bodyParser = require('koa-bodyparser');
app.use(bodyParser());

const router = require('./controller');

const {
    connect,
    initSchemas
} = require('./init');

(async () => {
    // 连接数据库
    await connect();
    // 初始化模型
    initSchemas();
})();

app.use(router.routes());
app.use(router.allowedMethods());



app.use(async (ctx) => {
    ctx.body = 'mongoDb对接成功';
});

app.listen(3001, () => {
    console.log('server run success');
});
